package com.chencl.cipher.service;

import com.chencl.cipher.config.DataSource;
import com.chencl.cipher.config.DataSourceEnum;
import com.chencl.cipher.config.log.OperationLogDetail;
import com.chencl.cipher.config.log.OperationType;
import com.chencl.cipher.config.log.OperationUnit;
import com.chencl.cipher.dao.Test1Mapper;
import com.chencl.cipher.dao.Test2Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Description: datasource
 * @EnglishName
 * @authod jiangshengli
 * @date 2019/6/27 18:03
 */
@Service
public class TestService {

    @Autowired
    private Test1Mapper test1Mapper;
    @Autowired
    private Test2Mapper test2Mapper;

    @OperationLogDetail(detail = "通过手机号[{{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
    @DataSource(DataSourceEnum.TEST1)
    public List<Map<String, Object>> findTest1() {

        // 不指定，则默认使用数据源1
        return test1Mapper.findAll();
    }



    // 覆盖类上指定的数据源2
    @DataSource(DataSourceEnum.TEST2)
    public List<Map<String, Object>> findTest2() {

        return test2Mapper.findAll();
    }

    /**
     * 如果切面不加@Order，添加Transactional注解会导致切换数据源失效
     */
    @Transactional
    @DataSource(DataSourceEnum.TEST2)
    public void insert() {
        test2Mapper.insert();
        int i = 1 / 0;
    }
}
